package com.ai.databao.web.controller.busi;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.ai.databao.service.impl.ILmzxQuerySV;
import com.ai.databao.web.controller.vo.LmzxQueryInfoVO;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.message.BasicNameValuePair;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.ai.databao.service.impl.IQueryUserInfoSV;
import com.ai.databao.web.controller.vo.QueryUserInfoVo;
import com.ai.databao.web.system.AppBaseController;

import net.sf.json.JSONObject;

/**
 * Created by huangzh on 17/4/13.
 */
@Controller
@RequestMapping(value = "/main")
public class MainController extends AppBaseController {
    private static final Logger log = Logger.getLogger(MainController.class);
    @Autowired
    private IQueryUserInfoSV queryUserInfoSV;

    @Autowired
    private ILmzxQuerySV limuQuerySV;
    /*
    * 功能描述:进入关键词维护页面
    * 开发人员:huangzh5
    * */
    @RequestMapping(value = "/pageInit")
    public String init(Model model, HttpServletRequest request, HttpServletResponse response){
        String url = "/index";// 返回页面

        return url;
    }

    @RequestMapping(value = "/personCredit")
    public String personCredit(Model model, HttpServletRequest request, HttpServletResponse response){
        String url = "/busi/credit/person_credit";// 返回页面

        return url;
    }

    @RequestMapping(value = "/personInform")
    public String personInform(Model model, HttpServletRequest request, HttpServletResponse response){
        String url = "/busi/credit/person-inform";// 返回页面

        return url;
    }

    @RequestMapping(value = "/personInformAuth")
    public String personInformAuth(Model model, HttpServletRequest request, HttpServletResponse response){
        String url = "/busi/credit/person-inform-authentification";// 返回页面

        return url;
    }

    @RequestMapping(value = "/bankAndNameInform")
    public String bankAndNameInform(Model model, HttpServletRequest request, HttpServletResponse response){
        String url = "/busi/credit/bank-name-inform";// 返回页面

        return url;
    }

    @RequestMapping(value = "/bankAndNameInformAuth")
    public String bankAndNameInformAuth(Model model, HttpServletRequest request, HttpServletResponse response){
        String url = "/busi/credit/bank-name-authentification";// 返回页面

        return url;
    }

    @RequestMapping(value = "/personPhone")
    public String personPhone(Model model, HttpServletRequest request, HttpServletResponse response){
        String url = "/busi/credit/person-phone-inform";// 返回页面

        return url;
    }

    @RequestMapping(value = "/personPhoneAuth")
    public String personPhoneAuth(Model model, HttpServletRequest request, HttpServletResponse response){
        String url = "/busi/credit/person-phone-authentification";// 返回页面

        return url;
    }

    @RequestMapping(value = "/orgInfo")
    public String orgInfo(Model model, HttpServletRequest request, HttpServletResponse response){
        String url = "/busi/credit/org-info-inform";// 返回页面

        return url;
    }

    @RequestMapping(value = "/orgInfoAuth")
    public String orgInfoAuth(Model model, HttpServletRequest request, HttpServletResponse response){
        String url = "/busi/credit/org-info-authentification";// 返回页面

        return url;
    }

    @RequestMapping(value = "/personCreditScore",method=RequestMethod.POST)
    public String personCreditScore(Model model, @ModelAttribute QueryUserInfoVo queryUserInfoVo,HttpServletRequest request, HttpServletResponse response) throws Exception{
    	//后期将用户请求查询的条件以单独的进程查询,以免得某一个接口拥堵时查询过慢
    	List<JSONObject> list = queryUserInfoSV.queryUserCredit(queryUserInfoVo);
    	model.addAttribute("userScore", list.toString());
        String url = "/busi/credit/person_credit_score";// 返回页面
        return url;
    }

    @RequestMapping(value = "/qryIdcardAndName",method=RequestMethod.POST)
    public JSONObject qryIdcardAndName(Model model, @ModelAttribute QueryUserInfoVo queryUserInfoVo, HttpServletRequest request, HttpServletResponse response) throws Exception{
        return queryUserInfoSV.qryIdcardAndName(queryUserInfoVo);
    }

    @RequestMapping(value = "/qryBankCardAndName",method=RequestMethod.POST)
    public JSONObject qryBankCardAndName(Model model, @ModelAttribute QueryUserInfoVo queryUserInfoVo, HttpServletRequest request, HttpServletResponse response) throws Exception{
        return queryUserInfoSV.qryBankCardAndName(queryUserInfoVo);
    }

    @RequestMapping(value = "/qryAllNetPhoneInfo",method=RequestMethod.POST)
    public JSONObject qryAllNetPhoneInfo(Model model, @ModelAttribute QueryUserInfoVo queryUserInfoVo, HttpServletRequest request, HttpServletResponse response) throws Exception{
        return queryUserInfoSV.qryAllNetPhoneInfo(queryUserInfoVo);
    }

    @RequestMapping(value = "/qryOrgInfo",method=RequestMethod.POST)
    public JSONObject qryOrgInfo(Model model, @ModelAttribute QueryUserInfoVo queryUserInfoVo, HttpServletRequest request, HttpServletResponse response) throws Exception{
        return queryUserInfoSV.qryOrgInfo(queryUserInfoVo);
    }

    @RequestMapping(value = "/qryOrgQualifications",method=RequestMethod.POST)
    public JSONObject qryOrgQualifications(Model model, @ModelAttribute QueryUserInfoVo queryUserInfoVo, HttpServletRequest request, HttpServletResponse response) throws Exception{
        return queryUserInfoSV.qryOrgQualifications(queryUserInfoVo);
    }

    @RequestMapping(value = "/qryOrgPenalty",method=RequestMethod.POST)
    public JSONObject qryOrgPenalty(Model model, @ModelAttribute QueryUserInfoVo queryUserInfoVo, HttpServletRequest request, HttpServletResponse response) throws Exception{
        return queryUserInfoSV.qryOrgPenalty(queryUserInfoVo);
    }

    @RequestMapping(value = "/qryOrgOwingTaxes",method=RequestMethod.POST)
    public JSONObject qryOrgOwingTaxes(Model model, @ModelAttribute QueryUserInfoVo queryUserInfoVo, HttpServletRequest request, HttpServletResponse response) throws Exception{
        return queryUserInfoSV.qryOrgOwingTaxes(queryUserInfoVo);
    }

    @RequestMapping(value = "/qryOrgAbnormal",method=RequestMethod.POST)
    public JSONObject qryOrgAbnormal(Model model, @ModelAttribute QueryUserInfoVo queryUserInfoVo, HttpServletRequest request, HttpServletResponse response) throws Exception{
        return queryUserInfoSV.qryOrgAbnormal(queryUserInfoVo);
    }

    @RequestMapping(value = "/qryOrgGenealogy",method=RequestMethod.POST)
    public JSONObject qryOrgGenealogy(Model model, @ModelAttribute QueryUserInfoVo queryUserInfoVo, HttpServletRequest request, HttpServletResponse response) throws Exception{
        return queryUserInfoSV.qryOrgGenealogy(queryUserInfoVo);
    }

    @RequestMapping(value = "/qryJudgment",method=RequestMethod.POST)
    public JSONObject qryJudgment(Model model, @ModelAttribute QueryUserInfoVo queryUserInfoVo, HttpServletRequest request, HttpServletResponse response) throws Exception{
        return queryUserInfoSV.qryJudgment(queryUserInfoVo);
    }

    //企业资质证书信息查询
    @RequestMapping(value = "/orgQualificationsInfo")
    public String orgQualificationsInfo(Model model, HttpServletRequest request, HttpServletResponse response){
        String url = "/busi/credit/org-qualifications-example";// 返回页面

        return url;
    }

    //企业资质证书信息查询
    @RequestMapping(value = "/orgQualificationsAct")
    public String orgQualificationsAct(Model model, HttpServletRequest request, HttpServletResponse response){
        String url = "/busi/credit/org-qualifications-act";// 返回页面

        return url;
    }

    //企业工商行政处罚信息查询
    @RequestMapping(value = "/orgPenaltyInfo")
    public String orgPenaltyInfo(Model model, HttpServletRequest request, HttpServletResponse response){
        String url = "/busi/credit/org-penalty-example";// 返回页面

        return url;
    }

    //企业工商行政处罚信息查询
    @RequestMapping(value = "/orgPenaltyAct")
    public String orgPenaltyAct(Model model, HttpServletRequest request, HttpServletResponse response){
        String url = "/busi/credit/org-penalty-act";// 返回页面

        return url;
    }

    //企业欠税信息查询
    @RequestMapping(value = "/orgOwingTaxesInfo")
    public String orgOwingTaxesInfo(Model model, HttpServletRequest request, HttpServletResponse response){
        String url = "/busi/credit/org-owingtaxes-example";// 返回页面

        return url;
    }

    //企业欠税信息查询
    @RequestMapping(value = "/orgOwingTaxesAct")
    public String orgOwingTaxesAct(Model model, HttpServletRequest request, HttpServletResponse response){
        String url = "/busi/credit/org-owingtaxes-act";// 返回页面

        return url;
    }

    //企业经营异常查询
    @RequestMapping(value = "/orgAbnormalInfo")
    public String orgAbnormalInfo(Model model, HttpServletRequest request, HttpServletResponse response){
        String url = "/busi/credit/org-abnormal-example";// 返回页面

        return url;
    }

    //企业经营异常查询
    @RequestMapping(value = "/orgAbnormalAct")
    public String orgAbnormalAct(Model model, HttpServletRequest request, HttpServletResponse response){
        String url = "/busi/credit/org-abnormal-act";// 返回页面

        return url;
    }

    //企业族谱查询
    @RequestMapping(value = "/orgGenealogyInfo")
    public String orgGenealogyInfo(Model model, HttpServletRequest request, HttpServletResponse response){
        String url = "/busi/credit/org-genealogy-example";// 返回页面

        return url;
    }

    //企业族谱查询
    @RequestMapping(value = "/orgGenealogyAct")
    public String orgGenealogyAct(Model model, HttpServletRequest request, HttpServletResponse response){
        String url = "/busi/credit/org-genealogy-act";// 返回页面

        return url;
    }

    //法院判决信息查询
    @RequestMapping(value = "/judgmentInfo")
    public String judgmentInfo(Model model, HttpServletRequest request, HttpServletResponse response){
        String url = "/busi/credit/org-judgment-example";// 返回页面

        return url;
    }

    //法院判决信息查询
    @RequestMapping(value = "/judgmentAct")
    public String judgmentAct(Model model, HttpServletRequest request, HttpServletResponse response){
        String url = "/busi/credit/org-judgment-act";// 返回页面

        return url;
    }

    @RequestMapping("/lmzxCreditInfo")
    public String pbcCreditInfo(Model model, HttpServletRequest request, HttpServletResponse response){
        String url = "/busi/lmzx/credit-example";
        return url;
    }

    @RequestMapping("/lmzxCreditAct")
    public String lmzxCreditAct(Model model, HttpServletRequest request, HttpServletResponse response){
        String url = "/busi/lmzx/credit-act";
        return url;
    }

    @RequestMapping(value="/qryLmzxCredit",method=RequestMethod.POST)
    public JSONObject qryLmzxCredit(Model model, @ModelAttribute LmzxQueryInfoVO queryInfoVo, HttpServletRequest request, HttpServletResponse response){
        String json = limuQuerySV.qryCredit(queryInfoVo);
        return JSONObject.fromObject(json);
    }

    @RequestMapping(value="/qryLmzxStatus",method=RequestMethod.POST)
    public JSONObject qryLmzxStatus(Model model, String token,String bizType, HttpServletRequest request, HttpServletResponse response){
        String json = limuQuerySV.qryStatus(token,bizType);
        return JSONObject.fromObject(json);
    }

    @RequestMapping(value="/qryLmzxResult",method=RequestMethod.POST)
    public JSONObject qryLmzxResult(Model model, String token,String bizType, HttpServletRequest request, HttpServletResponse response){
        String json = limuQuerySV.qryResult(token,bizType);
        return JSONObject.fromObject(json);
    }

    @RequestMapping(value="/socialSecurityInfo")
    public String socialSecurityInfo(Model model,  HttpServletRequest request, HttpServletResponse response){
        String url = "/busi/lmzx/social-security-example";
        return url;
    }

    @RequestMapping(value="/socialSecurityAct")
    public String socialSecurityAct(Model model,  HttpServletRequest request, HttpServletResponse response){
        String url = "/busi/lmzx/social-security-act";
        return url;
    }

    @RequestMapping(value="/qrySocialSecurityAreas",method=RequestMethod.POST)
    public JSONObject qrySocialSecurityAreas(Model model, @ModelAttribute LmzxQueryInfoVO queryInfoVo, HttpServletRequest request, HttpServletResponse response){
        String json = limuQuerySV.qrySocialSecurityAreas();
        return JSONObject.fromObject(json);
    }

    @RequestMapping(value="/qrySocialSecurityElem",method=RequestMethod.POST)
    public JSONObject qrySocialSecurityElem(Model model, String areaCode, HttpServletRequest request, HttpServletResponse response){
        String json = limuQuerySV.qrySocialSecurityElem(areaCode);
        return null;
        //return JSONObject.fromObject(json);
    }

    @RequestMapping(value="/qrySocialSecurity",method=RequestMethod.POST)
    public JSONObject qryLmzxResult(Model model,  HttpServletRequest request, HttpServletResponse response){
        List<BasicNameValuePair> reqParams = getReqParams(request);
        String json = limuQuerySV.qrySocialSecurity(reqParams);
        return JSONObject.fromObject(json);
    }

    @RequestMapping(value="/houseFundInfo")
    public String houseFundInfo(Model model,  HttpServletRequest request, HttpServletResponse response){
        String url = "/busi/lmzx/house-fund-example";
        return url;
    }

    @RequestMapping(value="/houseFundAct")
    public String houseFundAct(Model model,  HttpServletRequest request, HttpServletResponse response){
        String url = "/busi/lmzx/house-fund-act";
        return url;
    }

    @RequestMapping(value="/qryHouseFundAreas",method=RequestMethod.POST)
    public JSONObject qryHouseFundAreas(Model model, @ModelAttribute LmzxQueryInfoVO queryInfoVo, HttpServletRequest request, HttpServletResponse response){
        String json = limuQuerySV.qryHouseFundAreas();
        return JSONObject.fromObject(json);
    }

    @RequestMapping(value="/qryHouseFundElem",method=RequestMethod.POST)
    public JSONObject qryHouseFundElem(Model model, @ModelAttribute String areaCode, HttpServletRequest request, HttpServletResponse response){
        String json = limuQuerySV.qryHouseFundElem(areaCode);
        return JSONObject.fromObject(json);
    }

    @RequestMapping(value="/qryHouseFund",method=RequestMethod.POST)
    public JSONObject qryHouseFund(Model model,  HttpServletRequest request, HttpServletResponse response){
        List<BasicNameValuePair> reqParams = getReqParams(request);
        String json = limuQuerySV.qryHouseFund(reqParams);
        return JSONObject.fromObject(json);
    }

    /**
     * 获取以P_开头的参数
     * @param request
     * @return
     */
    private List<BasicNameValuePair> getReqParams(HttpServletRequest request){
        List<BasicNameValuePair> reqParams = new ArrayList<BasicNameValuePair>();
        Enumeration<String> enums =  request.getParameterNames();
        while (enums.hasMoreElements()){
            String paramName = enums.nextElement();
            if(paramName.startsWith("P_")){
                String reqParamName = paramName.substring(2);
                String reqParamValue = (String)request.getParameter(paramName);
                //密码要进行base64转码
                if(reqParamName.equals("password")){
                    try {
                        reqParamValue =  new String(Base64.encodeBase64(reqParamValue.getBytes("UTF-8")));
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
                reqParams.add(new BasicNameValuePair(reqParamName,reqParamValue));
            }
        }

        return reqParams;
    }
}
